import {
  Controller,
  Get,
  Req,
  Res,
  Post,
  Header,
  Redirect,
  Query,
  Body,
  Bind,
  HttpStatus,
} from '@nestjs/common';
import { AppService } from '../app.service';
import { CreateCatDto } from '../DTO/create-cat.dto';
import { QueryDto } from '../DTO/query.dto';
import { Response } from 'express';

@Controller('demo')
export class DemoController {
  constructor(private readonly appService: AppService) {}

  @Get('/demo1')
  f(): string {
    return 'this is demo1';
  }

  @Get('/demo1a')
  findAll(@Res() res: Response) {
    res.status(HttpStatus.OK).json([]);
  }

  @Post('/hua')
  @Bind(Res(), Body())
  ff(res, createCatDto) {
    console.log(createCatDto);
    res.status(HttpStatus.CREATED).send();
  }

  @Get('/demo2')
  getHello2(@Query() query: QueryDto): string {
    console.log(query)
    return 'this is demo2';
  }

  @Post('/demo11')
  create1(@Body() createCatDto: CreateCatDto) {
    console.log(createCatDto)
    return 'this is demo2';
  }

  @Post('/demo22')
  @Bind(Body())
  create2(createCatDto) {
    console.log(createCatDto)
    return 'this is demo2';
  }
  // 拿到原始的request对象
  @Get('/demo3')
  getHello3(@Req() request): object {
    console.log(request.query);
    return {
      name: 'hello3',
    };
  }

  // 拿到原始的response对象
  @Get('/demo4')
  @Header('Cache-Control', 'none')
  getHello4(@Res() res) {
    res.status(200).send('haha');
  }

  // handler 可使用 async 处理异步
  @Get('/async')
  @Header('Cache-Control', 'none')
  async getHello5(@Res() res) {
    res.status(200).send('haha');
  }

  @Get('docs')
  @Redirect('https://docs.nestjs.com', 302)
  getDocs(@Query('version') version) {
    if (version && version === '5') {
      return { url: 'https://docs.nestjs.com/v5/' };
    }
  }
}
